package ecnu.liwaTest.dd.pages;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

import java.util.List;

import static ecnu.liwaTest.dd.DriverSingleton.getChromeDriver;
import static ecnu.liwaTest.dd.DriverSingleton.waitForWindow;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

public class ConversationPage extends BasePage{

    WebDriver driver = getChromeDriver();

    public ConversationPage goConversationPage() {
        waitForWindow(5000);
        driver.findElement(By.cssSelector(".conversation-item-container")).click();
        waitForWindow(1000);
        assertThat(driver.getTitle(), is("会话列表"));
        return this;
    }

    public ConversationPage checkState(String state){
        waitForWindow(1000);
        String getState = driver.findElement(By.cssSelector(".states > p")).getText();
        assertThat(getState,is(state));
        return this;
    }

    public ConversationPage sendMessage(String message){

        waitForWindow(1000);
        driver.findElement(By.cssSelector(".text-input")).click();
        waitForWindow(1000);
        driver.findElement(By.cssSelector(".text-input")).sendKeys(message);
        waitForWindow(1000);
        driver.findElement(By.cssSelector(".text-input")).sendKeys(Keys.ENTER);
        waitForWindow(3000);

        List<WebElement> elements =  driver.findElements(By.className("message-send"));

        String mes = elements.get(elements.size()-1).getText();
        assertThat(mes,is(message));
        return this;
    }

    public ConversationPage sendIcon(){
        waitForWindow(1000);
        driver.findElement(By.cssSelector(".conversation-item-container")).click();
        waitForWindow(1000);
        driver.findElement(By.cssSelector(".icon-xiaolian")).click();
        waitForWindow(1000);
        driver.findElement(By.cssSelector(".emoji:nth-child(8) > img")).click();
        waitForWindow(1000);
        driver.findElement(By.cssSelector(".text-input")).sendKeys(Keys.ENTER);
        waitForWindow(1000);
        return this;
    }

    public ConversationPage sendEmptyMessage(){
        waitForWindow(1000);
        driver.findElement(By.cssSelector(".text-input")).sendKeys(Keys.ENTER);
        return this;
    }

    public ConversationPage endConversation(){

        waitForWindow(1000);
        driver.findElement(By.cssSelector(".conversation-item-container")).click();
        driver.findElement(By.cssSelector(".btns > div")).click();

        driver.findElement(By.cssSelector(".el-button--default:nth-child(2) > span")).click();
        waitForWindow(1000);
        driver.findElement(By.cssSelector(".el-button--default:nth-child(2) > span")).click();

        waitForWindow(3000);
        assertThat(driver.getTitle(), is("求助记录列表"));
        return this;
    }
}
